/*
 * @(#)CommentAction.java $version 2011. 10. 14
 * 
 * Copyright 2011 NHN Corp. All rights Reserved.
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.hangame.mbl.action.comment;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections15.map.HashedMap;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hangame.mbl.action.SmartPortalAction;
import com.hangame.mbl.bo.comment.CommentBO;
import com.hangame.mbl.constant.CommonConst;
import com.hangame.mbl.interceptor.checkreferer.CheckReferer;
import com.hangame.mbl.interceptor.expecthttpmethod.ExpectHttpMethod;
import com.hangame.mbl.model.Comment;
import com.hangame.mbl.model.HspUserInfo;
import com.hangame.mbl.util.CommonUtil;
import com.hangame.mbl.util.HspUserInfoUtil;
import com.nhncorp.hancore.action.AlertForward;
import com.nhncorp.lucy.security.permission.Permissions;
import com.nhncorp.lucy.web.metadata.Inject;
import com.nhncorp.lucy.web.metadata.Properties;

// TODO: Auto-generated Javadoc
/**
 * The Class CommentAction.
 *
 * @author NHN
 */
public class CommentAction extends SmartPortalAction {
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -5494566432293249883L;
	
	/** The Constant PAGESIZE. */
	private static final int PAGESIZE = 15;
	
	/** The log. */
	@SuppressWarnings("unused")
	private static Log log = LogFactory.getLog(CommentAction.class);
	
	/** The comment bo. */
	private CommentBO commentBO = null;

	/**
	 * Sets the comment bo.
	 * 
	 * @param commentBO the new comment bo
	 */
	@Inject(beanId = "commentBO")
	public void setCommentBO(CommentBO commentBO) {
		this.commentBO = commentBO;
	}

	/* (non-Javadoc)
	 * @see com.hangame.mbl.action.SmartPortalAction#execute()
	 */
	@Override
	public String execute() throws Exception {
		int bbsNo = NumberUtils.toInt(params.getString("bbsNo"), -1);
		int cPageNo = NumberUtils.toInt(params.getString("cPageNo"), 1);

		getList(bbsNo, cPageNo, false);
		return "comment";
	}

	/**
	 * Gets the list.
	 * 
	 * @param bbsNo the bbs no
	 * @param cPageNo the c page no
	 * @param isLastPage the is last page
	 * @return the list
	 */
	private void getList(int bbsNo, int cPageNo, boolean isLastPage) {
		int commentCount = commentBO.getCommentCount(bbsNo);
		//마지막 페이지로 세팅(코멘트 입력시), 마지막페이지보다 페이지번호가 크면 한페이지 줄임(코멘트 삭제시)
		int lastPage = (int)Math.ceil((float)commentCount / (float)PAGESIZE);
		if (isLastPage || cPageNo > lastPage) {
			cPageNo = lastPage;
		}
		int offset = CommonUtil.calcOffset(cPageNo, PAGESIZE);

		List<Comment> commentList = commentBO.getCommentList(bbsNo, offset, PAGESIZE);
		if (commentCount > cPageNo * PAGESIZE) {
			setAttribute("hasNextPage", true);
		}
		setAttribute("commentCount", commentCount);
		setAttribute("cPageNo", cPageNo);
		setAttribute("commentList", commentList);
		setAttribute("myProfPicUrl", getMyProfPicUrl());
	}

	/**
	 * Write.
	 * 
	 * @return the string
	 */
	@Permissions(required = "login")
	@Properties(values = "popup=true")
	@ExpectHttpMethod(method = "post")
	@CheckReferer
	public String write() {
		int bbsNo = params.getInt("bbsNo");
		String userId = getUser().getUserId();
		String content = params.getString("content");

		int result = commentBO.writeComment(bbsNo, userId, content);
		if (result != 1) {
			return AlertForward.alertAndBack("common.exception.failProcess");
		} else {
			getList(bbsNo, 1, false);
		}
		return "comment";
	}

	/**
	 * Delete.
	 * 
	 * @return the string
	 */
	@Permissions(required = "login")
	@Properties(values = "popup=true")
	@ExpectHttpMethod(method = "post")
	@CheckReferer
	public String delete() {
		int bbsNo = params.getInt("bbsNo");
		String userId = getUser().getUserId();
		int commentNo = params.getInt("commentNo");
		int cPageNo = NumberUtils.toInt(params.getString("cPageNo"), 1);

		int result = commentBO.deleteComment(bbsNo, userId, commentNo);

		if (result != 1) {
			return AlertForward.alertAndBack("common.exception.failProcess");
		} else {
			getList(bbsNo, cPageNo, false);
		}
		return "comment";
	}

	/**
	 * 로그인된 사용자의 hsp 프로필 이미지 URL을 가져옴.
	 * 
	 * @return the my prof pic url
	 */
	private String getMyProfPicUrl() {
		String result = "";

		if (getUser().isLoggedin()) { //로그인된 경우
			String userId = getUser().getUserId();

			Map<String, String> userIdMap = new HashedMap<String, String>();
			userIdMap.put(userId, userId);
			Map<String, HspUserInfo> hspUserInfoMap = HspUserInfoUtil.getHspUserInfo(userIdMap);

			if (hspUserInfoMap != null) {
				HspUserInfo hspUserInfo = hspUserInfoMap.get(userId);
				if (hspUserInfo != null) {
					result = CommonConst.MPHOTO_PREFIX_URL + hspUserInfoMap.get(userId).getsNo() + CommonConst.MPHOTO_SUFFIX_URL;
				} else {
					result = CommonConst.MPHOTO_PREFIX_URL + 0 + CommonConst.MPHOTO_SUFFIX_URL;
				}
			}

			return result;
		} else {
			return "";
		}
	}
}
